﻿using System.Text;
using Kwan.Helper.Strings;
using System.Management;
using Kwan.Core;

namespace Kwan.Data.Validates
{
    public class RSACheck
    {
        public static bool pass;
        static RSACheck()
        {
            try
            {
                RasCrypto rsa = new RasCrypto();
                rsa.PublicKey = FileHelper.GetFileString("Kwan");
                string signKey = FileHelper.GetFileString("Kwans");
                string data = GetMachineInfo();
                pass = rsa.Verify(data, signKey);
            }
            catch
            {
                pass = false;
            }
        }

        //获取数据
        private static string GetMachineInfo()
        {
            StringBuilder sb = new StringBuilder();
            ManagementObjectSearcher my = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
            foreach (ManagementObject share in my.Get())
            {
                sb.Append(share["Manufacturer"].ToString());
                sb.Append(share["Product"].ToString());
                sb.Append(share["SerialNumber"].ToString());
            }

            //得到cpu序列号 
            string strCPUNo = string.Empty;
            ManagementClass cimObject = new ManagementClass("Win32_Processor");
            ManagementObjectCollection mocHard = cimObject.GetInstances();
            foreach (ManagementObject moHard in mocHard)
            {
                strCPUNo = moHard.Properties["ProcessorId"].Value.ToString();
            }
            sb.Append(strCPUNo);

            //硬盘序列号 
            ManagementObject m_objDisk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
            sb.Append((string)m_objDisk.GetPropertyValue("VolumeSerialNumber"));
            return sb.ToString();
        }
    }
}
